from django.http import HttpResponse
from django.shortcuts import render
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.views import APIView

from api import models
from ext.throttle import MyThrottle
from rest_framework.versioning import QueryParameterVersioning
from django.forms import ModelForm
ModelForm.is_valid()

# Create your views here.
def login(request):
    return HttpResponse("Hello, world. You're at the")

class User(APIView):
    throttle_classes = [MyThrottle]
    def get(self, request):
        return Response({"message": "Hello, world!"})

class Home(APIView):
    versioning_class = QueryParameterVersioning
    def get(self, request):
        return Response("hahahh")

class DpSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Depart
        fields = "__all__"
class Dp2Serializer(serializers.ModelSerializer):
    class Meta:
        model = models.Depart
        fields = ["title","order","count"]
class DpView(APIView):
    def post(self, request,*args, **kwargs):
        ser = DpSerializer(data=request.data)
        if ser.is_valid():
            instance = ser.save()
            # print(instance)
            xx = Dp2Serializer(instance=instance)
            return Response(xx.data)
        else:
            # print("视图",ser.errors)
            return Response(ser.errors)

class UusSerializer(serializers.ModelSerializer):
    # genderinfo = serializers.SerializerMethodField()
    v1 = DpSerializer(read_only=True,source="depart")
    class Meta:
        model = models.Course
        fields = ["id","name","age","gender","depart","v1"]
        extra_kwargs = {
            "id":{"read_only": True},
            # "gender":{"write_only": True}
        }
    # def get_genderinfo(self,obj):
    #     return {"id": obj.gender, "text": obj.get_gender_display()}
    def get_v1(self,obj):
        return obj
class UusView(APIView):
    def post(self, request,*args, **kwargs):
        ser = UusSerializer(data=request.data)
        if ser.is_valid():
            ser.save()    #保存时会自动生成instance对象
            # print(instance)
            # xx = UusSerializer(instance=instance)
            return Response(ser.data)
        else:
            # print("视图",ser.errors)
            return Response(ser.errors)


class NnSerializer(serializers.ModelSerializer):
    gender = serializers.SerializerMethodField()
    name = serializers.CharField(read_only=True)
    v1 = DpSerializer(read_only=True,source="depart")
    class Meta:
        model = models.NbCourse
        fields = ["id","name","age","gender"]
        extra_kwargs = {
            "id":{"read_only": True},
        }

class NbView(APIView):
    from rest_framework.pagination import PageNumberPagination
    def post(self, request,*args, **kwargs):
        ser = NnSerializer(data=request.data)
        if ser.is_valid():
            ser.save()    # 保存时会自动生成instance对象
            return Response(ser.data)
        else:
            return Response(ser.errors)